// index page
// ============

define([ 'require', 'zepto', 'wx', 'dropwizard', 'sui'],
    function(require, $, wx, dropwizard) {
      return {
        init : function(data) {
          this.initPage();
          this.gotoOrder();
        },
        
        initPage: function() {
          $('#drop').dropwizard()

          $('#drop').on('shown:fancy:dropwizard', function(e) {
            console.log(e)
          })
        },
        
        /**
         * Check mobile value is valid or not
         */
        isMobile: function(mobile) {
          return mobile && /^((13)|(16)|(15)|(17)|(18))\d{9}$/.test(mobile)
        },
        
        /**
         * Check mobile value is valid or not
         */
        isEmail: function(email) {
          var reg = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
          return email && reg.test(email)
        },
        
        isPinyin: function(pinyin) {
          var reg = /^(?!_)([A-Za-z ]+)$/;
          var i = pinyin && reg.test(pinyin);
          return i;
        },
        
        gotoOrder: function(e) {
          var that = this;
          $(".userinfo-link").on("click", function(e){
            e.preventDefault();
            var href= $(this).data("href");
            var $form = $("#orderForm");
            
            var $firstName = $("input[name='firstName']");
            var $lastName = $("input[name='lastName']");
            var $userPhone = $("input[name='userPhone']");
            var $userEmail = $("input[name='userEmail']");
            var $airportNumber = $("input[name='airportNumber']");
            
            if(!$firstName.val() || !$lastName.val() || !that.isPinyin($firstName.val()) || !that.isPinyin($lastName.val())) {
              $.toast("请输入正确的用户姓名拼音");
              return
            }
            
            if(!$userPhone.val() || !that.isMobile($userPhone.val())) {
              $.toast("请输入正确的用户手机");
              return
            }
            
            if(!$userEmail.val() || !that.isEmail($userEmail.val())) {
              $.toast("请输入正确的用户邮箱");
              return
            }
            
            if($airportNumber.val() && !(/^[a-zA-Z0-9]{4,}$/.test($airportNumber.val()))) {
              $.toast("请输入正确的航班号");
              return
            }
            
            $.showPreloader('正在提交用户信息');
            
            $("input[name='firstName']").val($("input[name='firstName']").val().toUpperCase())
            $("input[name='lastName']").val($("input[name='lastName']").val().toUpperCase())
            // serialize parameters
            var parameters = $form.serializeArray()
            
            $.ajax({
              type: "POST",
              url: href,
              dataType: "json",
              data: parameters,
              success: function(data) {
                $.hidePreloader();
                if(data.code == 0) {
                  var orderId = data.data.order.id;
                  window.location.href = window.__CTX__.root + "/shopping/orders/" + orderId
                } else {
                  $.toast(data.message);
                }
              },
              error: function() {
                $.toast("创建失败");
              }
            })
          })
        }
      }
    })